const ERR_CODE = 50000;
const SUCC_CODE = 20000;

class Result {
    constructor() {
        this.code = -1;
        this.msg = null;
        this.data = null;
    }
    dataJson(res) {
        return res.json({
            code: this.code,
            msg: this.msg,
            data: this.data
        })
    }
    // 成功返回
    success(res, ...rest) {
        this.code = SUCC_CODE;
        this.msg = rest[0] == 'SUCCESS' ? rest[1] : 'ok';
        if (rest.length == 1) this.data = rest[0]
        return this.dataJson(res)
    }
    // 失败返回
    fail(res, msg) {
        this.code = ERR_CODE;
        this.msg = msg ? msg : 'error';
        return this.dataJson(res)
    }
}

module.exports = new Result